﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsWeb
{
    public class SendMail
    {
        private static SendMail _Current;
        public static SendMail Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new SendMail();
                }
                return _Current;
            }
        }

        private string mailService = System.Configuration.ConfigurationSettings.AppSettings["mailService"];
        private string fromMailName = System.Configuration.ConfigurationSettings.AppSettings["fromMailName"];
        private string fromMailNamePassword = System.Configuration.ConfigurationSettings.AppSettings["fromMailNamePassword"];
        private string toMailName = System.Configuration.ConfigurationSettings.AppSettings["toMailName"];
        public void sendMail(string subject, string body, string attachment)
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = mailService;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(fromMailName, fromMailNamePassword);
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(fromMailName, toMailName);
            message.Subject = subject;
            message.Body = body;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            if (attachment != null && attachment != "")
            {
                System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(attachment, System.Net.Mime.MediaTypeNames.Application.Octet);
                message.Attachments.Add(data);
            }
            try
            {
                client.Send(message);
            }
            catch
            {
            }
        }
    }
}
